from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# 加载糖尿病数据集
diabetas = datasets.load_diabetes()
x = diabetas.data
y = diabetas.target

# 将数据集拆分为训练集和测试集
X_train, X_test, Y_train, Y_test =  train_test_split(x, y, test_size=0.2)

#创建一个多元线性回归算法对象
lr = LinearRegression()

#使用训练集训练模型
lr.fit(X_train, Y_train)

# 使用测试集来进行预测
y_pred_train = lr.predict(X_train)
y_pred_test = lr.predict(X_test)

#打印
print("训练集均方误差：%.2f" % mean_squared_error(Y_train,y_pred_train))
print("测试集均方误差：%.2f" % mean_squared_error(Y_test,y_pred_test))